package cn.zwx.strategy.pattern.example;

import cn.zwx.strategy.pattern.example.basic.BasicService;
import cn.zwx.strategy.pattern.example.simple.SimpleService;
import cn.zwx.strategy.pattern.example.strategy.StrategyService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @projectName:strategy-pattern-example
 * @see:cn.zwx.strategy.pattern.example
 * @author:zhangwenxue
 * @createTime:2022/2/11 10:07
 * @version:1.0
 */
@RestController
public class DiscountCouponController {

    @Resource
    private SimpleService simpleService;

    @Resource
    private BasicService basicService;

    @Resource
    private StrategyService strategyService;


    /**
     *  简单的switch实现
     * @param resourceName 资源名称
     * @return java.lang.String
     * @author zhangwenxue
     * @createTime 2022/2/11 10:09
     **/
    @RequestMapping("/simple/grantType")
    public String simple(String resourceName){
       return simpleService.getResult(resourceName);
    }

    /**
     *  使用策略模式实现
     * @param resourceName 资源名称
     * @return java.lang.String
     * @author zhangwenxue
     * @createTime 2022/2/11 10:09
     **/
    @RequestMapping("/basic/grantType")
    public String basic(String resourceName){
        return basicService.getResult(resourceName);
    }


    /**
     *  Map+函数式接口通过Map.get(key)来代替
     *  if-else的业务分派，
     *  能够避免策略模式带来的类增多、
     *  难以俯视整个业务逻辑的问题。
     * @param resourceName 资源名称
     * @return java.lang.String
     * @author zhangwenxue
     * @createTime 2022/2/11 10:09
     **/
    @RequestMapping("/strategy/grantType")
    public String strategy(String resourceName){
        return strategyService.getResult(resourceName);
    }
}
